package com.itheima.controller.backend.controller;

import com.itheima.common.dto.SetmealDto;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.ISetmealService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 套餐Controller
 *
 * @author itheima
 * @date 2025-03-23
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    private static final Logger log = LoggerFactory.getLogger(SetmealController.class);
    @Autowired
    private ISetmealService setmealService;

/*    *//**
     * 查询套餐列表
     *
     * @return
     *//*
    @PostMapping("/getSetmeal")
    public Result getSetmeal() {
        List<Setmeal> setmealList = setmealService.selectList();
        return new Result(true, "查询套餐数据成功", setmealList);
    }*/

    /**
     * 查询套餐详情
     *
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public Result findById(Integer id) {
        Setmeal setmeal = setmealService.selectDetilesById(id);
        return new Result(true, "查询套餐成功", setmeal);
    }

    //图片上传
    @PostMapping("/upload")
    public Result upload(MultipartFile imgFile) {
        String url = setmealService.upload(imgFile);
        log.info("图片上传成功，图片地址为：{}", url);
        return new Result(true, "图片上传成功", url);
    }

    /**
     * 分页查询套餐列表
     */
    @PostMapping("/findPage")
    public PageResult findPage(@RequestBody(required = false) SetmealDto setmealDao) {
        return setmealService.selectSetmealList(setmealDao);
    }

    /**
     * 获取套餐详细信息
     */
    @GetMapping("/{id}")
    public Setmeal getInfo(@PathVariable("id") Integer id) {
        return setmealService.selectSetmealById(id);
    }

    /**
     * 新增套餐
     */
    @PostMapping("/add")
    public Result add(@RequestParam List<Integer> checkgroupIds, @RequestBody Setmeal setmeal) {
        setmealService.insertSetmeal(checkgroupIds,setmeal);
        return  new Result(true, "新增套餐成功", null);
    }

    @PostMapping("/edit")
    public Result edit(@RequestParam List<Integer> checkGroupIds,@RequestBody Setmeal setmeal) {
        setmealService.updateSetmeal(checkGroupIds,setmeal);
        return  new Result(true, "编辑套餐成功", null);
    }

    @GetMapping("/delete")
    public void delete(@RequestParam Integer[] ids) {
        setmealService.deleteSetmealByIds(ids);
    }

}
